#encoding=utf-8
import ujson as json
import base64
from pprint import pprint as pp
key = 13776953
key2 = "ASCGRTHNJSWEASCGRTHNJSWEASCGRTHNJSWE"

def Encode(_data,_interface=""):
    print "Encode",_interface
    t_strs = json.dumps(_data);
    if _interface in ("push","",None,"timer"):
        return t_strs
    t_key = INTERFACE_KEY_DICT.get(_interface,key)
    t_strs = base64.urlsafe_b64encode(t_strs)

    t_cut = t_key%len(t_strs)
    return t_strs[t_cut:]+t_strs[:t_cut]

def Encode_OSS(_data,_interface=""):
    t_strs = json.dumps(_data);
    t_key = INTERFACE_KEY_DICT.get(_interface,key)
    t_strs = base64.urlsafe_b64encode(t_strs)

    t_cut = t_key%len(t_strs)
    #print t_strs[t_cut:]+t_strs[:t_cut]
    return t_strs[t_cut:]+t_strs[:t_cut]

def Decode_OSS(t_strs,_interface=""):
    '''if _interface == "":
        return json.loads(t_strs)'''
    #return json.loads(t_strs)
    print "_interface:", _interface
    t_key = INTERFACE_KEY_DICT.get(_interface,key)
    print "Decode_OSS t_key:", t_key
    t_cut = len(t_strs) - (t_key%len(t_strs))
    #print t_strs[0:t_cut]+t_strs[t_cut:]
    t_strs = t_strs[t_cut:]+t_strs[0:t_cut]
    print "Decode_OSS t_strs11:", t_strs
    try:
        t_strs = base64.urlsafe_b64decode(t_strs)
    except:
        t_strs = base64.urlsafe_b64decode(t_strs+"==")

    print "Decode_OSS t_strs22:", t_strs
    try:
        t_ret = json.loads(t_strs)
    except:
        assert False,t_strs

    return t_ret

def Decode(t_strs,_interface=""):

    if _interface in ("push","",None,"timer"):
        try:
            return json.loads(t_strs)
        except:
            return []

    t_key = INTERFACE_KEY_DICT.get(_interface,key)
    #print t_strs, t_key
    #print '----------------1---------------'
    t_cut = len(t_strs) - (t_key%len(t_strs))
    #print '----------------2---------------'
    try:
        #print '----------------3---------------'
        t_strs = base64.urlsafe_b64decode(t_strs[t_cut:]+t_strs[0:t_cut])
    except:
        #print '----------------4---------------'
        t_strs = base64.urlsafe_b64decode(t_strs[t_cut:]+t_strs[0:t_cut]+"==")
    try:
        t_ret = json.loads(t_strs)
    except:
        assert False,t_strs
    #print '----------------5---------------'
    return t_ret


INTERFACE_KEY_DICT={
    "oss":1234567890,
    "timer":1234567890,
    "active":77655213,
    "push":0,
}

if __name__ == "__main__":
    #print Encode_OSS(json.dumps({"m": "EXTERNAL_GWINFO", "p":[1]}),"oss")
    #print json.dumps({"m": "EXTERNAL_GWINFO"})

    print base64.urlsafe_b64encode('{"m":"EXTERNAL_GWINFO","p":[1]}')
    print "eyJtIjoiRVhURVJOQUxfR1dJTkZPIiwicCI6WzFdfQ"
    print base64.urlsafe_b64decode("eyJtIjoiRVhURVJOQUxfR1dJTkZPIn0==")
    #print Encode_OSS("123456","oss")
    #print Decode_OSS("eyJtIjoiRVhURVJOQUxfR1dJTkZPIiwicCI6WzFdfQ","oss")
#print Decode_OSS("JBQ0NPVU5UX1JFRElTIjoiQUNDT1VOVF9SRURJUyIsIk1BWF9TRVJWRVJfVVNFUiI6NTAwMCwiQ09OTkVDVF9JUFBPUlQiOiIxOTIuMTY4LjEuMjM4OjgwMDgiLCJDTElFTlRfVkVSU0lPTiI6IkRFRkFVTFRfQ0xJRU5UX1ZFUlNJT05SQU5HRSIsIk5BTUUiOiJ0aW5zXHU2ZDRiXHU4YmQ1IiwiSk9CUyI6dHJ1ZSwiSVNPUEVOIjp0cnVlLCJKT0JTS0VZIjoiSEFHT09UX1NFUlZFUl9XV1ciLCJGTEFHIjoibmV3IiwiU09SVCI6MX19fX1dW3sibSI6IkVYVEVSTkFMX1NBVkVfU0VSVkVSX0NPTkZJRyIsImRhdGEiOnsidGluczEiOnsidGluczEiOnsiREVCVUciOjAsIk9QRU5EQVRFIjoiMjAxNC0wNy0xNSIsIlJFRElTIjoiMTI3LjAuMC4xOjYzODEiLC",'oss')
    a='FhMTIzNDU2XHVmZjA4XHU5MDAxXHU3ZWEyXHU1MzA1XHVmZjA5IiwiZW5kdGltZSI6MTUwNjQ5NDI2NywiaWQiOiIxIn1dLCJyIjowfSwiYyI6MSwibSI6InIifV0=W3sicCI6eyJtc2dsaXN0IjpbeyJzb3J0IjoxLCJwbGF0Zm9ybSI6ImFsbCIsInN0YXJ0dGltZSI6MTUwNjQ5NDIwNywibXNnIjoiXHU2ZDRiXHU4YmQ1XHU4ZGQxXHU5YTZjXHU3MDZmXHVmZj'
    b=123
    #counmer
    #{u'uid': u'f19c89fea2bc11e79f33e89a8fcd41d7', u'area': u'test1', u'pid': u'TEST', u'reason': u'BA_FNBATTLE', u'gid': u'mzwsw', u'data': u'[{"amt":52,"unit":"gamepoint","created":1506583190.7919559479},{"amt":26,"unit":"gamepoint","created":1506583190.7954668999}]', u'order': u'6a9c55c7a41d11e79f33e89a8fcd41d7'}
    a='N3IiwidWlkIjoiZjE5Yzg5ZmVhMmJjMTFlNzlmMzNlODlhOGZjZDQxZDciLCJhcmVhIjoidGVzdDEiLCJkYXRhIjoiW3tcImFtdFwiOjUyLFwidW5pdFwiOlwiZ2FtZXBvaW50XCIsXCJjcmVhdGVkXCI6MTUwNjU4MzE5MC43OTE5NTU5NDc5fSx7XCJhbXRcIjoyNixcInVuaXRcIjpcImdhbWVwb2ludFwiLFwiY3JlYXRlZFwiOjE1MDY1ODMxOTAuNzk1NDY2ODk5OX1dIiwicGlkIjoiVEVTVCIsIm9yZGVyIjoiNmE5YzU1YzdhNDFkMTFlNzlmMzNlODlhOGZjZDQxZDcifQ==eyJyZWFzb24iOiJCQV9GTkJBVFRMRSIsImdpZCI6Im16d3'
    #login
    #{u'uid': u'f19c89fea2bc11e79f33e89a8fcd41d7', u'area': u'test1', u'pid': u'TEST', u'reason': u'U_FLOGIN', u'gid': u'mzwsw', u'data': u'[{"ip":"192.168.0.123","created":1506583179.4030508995}]', u'order': u'63c6ebdda41d11e79f33e89a8fcd41d7'}
    a='VhMmJjMTFlNzlmMzNlODlhOGZjZDQxZDciLCJhcmVhIjoidGVzdDEiLCJkYXRhIjoiW3tcImlwXCI6XCIxOTIuMTY4LjAuMTIzXCIsXCJjcmVhdGVkXCI6MTUwNjU4MzE3OS40MDMwNTA4OTk1fV0iLCJwaWQiOiJURVNUIiwib3JkZXIiOiI2M2M2ZWJkZGE0MWQxMWU3OWYzM2U4OWE4ZmNkNDFkNyJ9eyJyZWFzb24iOiJVX0ZMT0dJTiIsImdpZCI6Im16d3N3IiwidWlkIjoiZjE5Yzg5Zm'
    #online
    a='YzYzZlYmUxYTQxZDExZTc5ZjMzZTg5YThmY2Q0MWQ3In0=eyJyZWFzb24iOiJCX0ZSQUciLCJnaWQiOiJtendzdyIsInVpZCI6ImYxOWM4OWZlYTJiYzExZTc5ZjMzZTg5YThmY2Q0MWQ3IiwiYXJlYSI6InRlc3QxIiwiZGF0YSI6Ilt7XCJjcmVhdGVkXCI6MTUwNjU4MzE3OS41OTU0OTgwODV9XSIsInBpZCI6IlRFU1QiLCJvcmRlciI6Ij'
    print '--->',Decode(a, "oss")
    
